package com.zhdl.modules.prefiresys.web.service;

import com.project.network.entity.PortComm;
import com.project.network.entity.PortSocket;
import com.project.network.service.PortCommService;
import com.project.network.service.PortSocketService;
import com.zhdl.modules.prefiresys.entity.Detector;
import com.zhdl.modules.prefiresys.entity.DetectorAreaInfo;
import com.zhdl.modules.prefiresys.entity.FactoryLocalHost;
import com.zhdl.modules.prefiresys.service.DetectorAreaInfoService;
import com.zhdl.modules.prefiresys.service.DetectorService;
import com.zhdl.modules.prefiresys.service.FactoryLocalHostService;
import com.zhdl.modules.prefiresys.service.dto.BaseQueryCriteria;
import com.zhdl.modules.prefiresys.web.response.AreaRealData;
import com.zhdl.modules.prefiresys.web.response.HomeSseRespData;
import com.zhdl.modules.prefiresys.web.response.TcpSerialResp;
import com.zhdl.network.prefiretcp.FramePkg;
import com.zhdl.network.prefiretcp.before.MemDevice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledFuture;

@Slf4j
@Component
public class CollectService {

    private final DetectorService detectorService;
    private final DetectorAreaInfoService detectorAreaInfoService;
    private final FactoryLocalHostService factoryLocalHostService;

    public Map<String, MemDevice> memDeviceList = new HashMap<>();
    /**
     * 首页信息
     */
    public HomeSseRespData sseResp = new HomeSseRespData();

    /**
     * 当前收到数据的时间
     */
    public static Map<String,String> currentTime = new HashMap<>();

    /**
     * 本地主机
     */
    public FactoryLocalHost factoryLocalHost = null;

    /**
     * 初始化
     *
     * @param detectorService
     * @param detectorAreaInfoService
     * @param factoryLocalHostService
     */
    private CollectService(DetectorService detectorService, DetectorAreaInfoService detectorAreaInfoService,
                           FactoryLocalHostService factoryLocalHostService) {
        this.detectorService = detectorService;
        this.detectorAreaInfoService = detectorAreaInfoService;
        this.factoryLocalHostService = factoryLocalHostService;
    }

    /**
     * 从数据库加载设备到内存
     */
    public void BuildMemDev() {
        /*
          获取设备防区信息
         */
        List<DetectorAreaInfo> dbAreaList = detectorAreaInfoService.selectAll();
        Map<String, AreaRealData> areaList = new HashMap<>();
        for (DetectorAreaInfo area : dbAreaList) {
            if (area.isStatus()){
                AreaRealData areaMem = new AreaRealData();
                areaMem.setAreaName(area.getAreaName());
                areaMem.setAreaNo(area.getAreaNo());
                areaMem.setDetectorAddr(area.getDetectorAddr());
                areaList.put(area.getId(), areaMem);
            }
        }
        sseResp.setAreaList(areaList);
        sseResp.setAreaCount(areaList.size());

        /*
          获取探测器设备信息
         */
        List<Detector> list = detectorService.selectAll();

        for (Detector det : list) {

            /*
              获取探测器信息
             */
            MemDevice device = new MemDevice();


            device.setDeviceId(det.getId());
            device.setDetectorAddr(det.getDetectorAddr());
            device.setEncrypt(det.isCommEncrypt());
            device.setSeqInHost(det.getSeqInHost());

            device.setPkg(new FramePkg(device.getDetectorAddr(), device.isEncrypt()));

            memDeviceList.put(device.getDeviceId(), device);

            /*
              获取探测器防区信息
             */
            DetectorAreaInfo area1Entity = detectorAreaInfoService.findByAreaNo(det.getId(), (byte) 1);
            device.setArea1Id(area1Entity.getId());
            memDeviceList.put(det.getId(), device);
        }

        sseResp.setDeviceCount(list.size());

        /*
              获取本地主机信息
             */
        List<FactoryLocalHost> listHost = factoryLocalHostService.selectAll();

        if (listHost.size() > 0) {
            factoryLocalHost = listHost.get(0);
        }

    }

    /**
     * 通过id获取设备内存
     *
     * @param id
     * @return
     */
    public MemDevice getMemDevice(String id) {
        return memDeviceList.get(id);
    }

}